let upload = require('../../common/upload')
Page({
    behaviors: [upload],
    data: {
        introduction: '',
        address: '',
        addressErr: false,
        house: '',
        houseErr: false,
        region: [],
        province: '',
        province_code: '',
        city: '',
        city_code: '',
        district: '',
        county_code: '',
        custom: '',
        customErr: false,
        logo: '',
        logoErr: false,
        logoFormData: { from: 'comment' },
        front_ID: [], //身份证正面
        front_IDErr: false,
        front_IDFormData: { from: 'comment' },
        reverse_ID: [], //身份证反面
        reverse_IDErr: false,
        reverse_IDFormData: { from: 'comment' },
        buttonLoading: false,
        agreement: true, //同意协议
    },
    onLoad() {
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.on('acceptDataFromOpenerPage', ({ supplierInfo }) => {
            this.setData({
                introduction: supplierInfo.intro,
                address: supplierInfo.province + supplierInfo.city + supplierInfo.district,
                house: supplierInfo.addr,
                region: [supplierInfo.province, supplierInfo.city, supplierInfo.district],
                province: supplierInfo.province,
                province_code: supplierInfo.province_code,
                city: supplierInfo.city,
                city_code: supplierInfo.city_code,
                district: supplierInfo.district,
                county_code: supplierInfo.district_code,
                custom: supplierInfo.phone
            })
            if (supplierInfo.face) {
                this.setData({
                    logo: [{
                        type: 'image',
                        status: 'done',
                        uploadPath: supplierInfo.face,
                        url: this.data.imgHost + supplierInfo.face
                    }]
                })
            }

            if (supplierInfo.id_photo_back) {
                this.setData({
                    reverse_ID: [{
                        type: 'image',
                        status: 'done',
                        uploadPath: supplierInfo.id_photo_back,
                        url: this.data.imgHost + supplierInfo.id_photo_back
                    }]
                })
            }

            if (supplierInfo.id_photo_front) {
                this.setData({
                    front_ID: [{
                        type: 'image',
                        status: 'done',
                        uploadPath: supplierInfo.id_photo_front,
                        url: this.data.imgHost + supplierInfo.id_photo_front
                    }]
                })
            }
        })
    },
    submit() {
        const { introduction, address, house, front_ID, reverse_ID, logo, custom, province_code, province, city_code, city, county_code, district, agreement } = this.data
        if (address === '') return this.setData({ addressErr: true })
        if (house === '') return this.setData({ houseErr: true })
        if (custom === '') return this.setData({ customErr: true })
        if (custom.length !== 11) {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'none'
            })
            return this.setData({ customErr: true })
        }
        if (logo.length === 0) return this.setData({ logoErr: true })
        if (front_ID.length === 0) return this.setData({ front_IDErr: true })
        if (reverse_ID.length === 0) return this.setData({ reverse_IDErr: true })

        let flag = true
        let field = ['logo', 'front_ID', 'reverse_ID']
        for (let list = 0; list < field.length; list++) {
            let data = this.data[field[list]]
            for (let i = 0; i < data.length; i++) {
                if (data[i].status !== 'done') {
                    flag = false
                    break;
                }
            }
        }
        if (!flag) return wx.showToast({
            title: '有图片正在上传中或者上传失败',
            icon: 'none',
            duration: 2000
        })

        if (!agreement) return wx.showToast({
            title: '请阅读并同意用户协议',
            icon: 'none',
            duration: 2000
        })
        this.setData({ buttonLoading: true })

        let reqData = {
            face: logo[0].uploadPath,
            phone: custom,
            province_code: province_code,
            province: province,
            city_code: city_code,
            city: city,
            district_code: county_code,
            district: district,
            addr: house,
            // house: house,
            id_photo_front: front_ID[0].uploadPath,
            id_photo_back: reverse_ID[0].uploadPath,
        }
        if (introduction) reqData.intro = introduction

        this.request({
            url: '/supplier/supplier/update',
            method: 'POST',
            data: reqData
        }, () => {
            wx.showToast({
                title: '修改成功',
                icon: 'success',
                mask: true
            })
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit('refresh')
            this.delay(() => {
                wx.navigateBack()
            })
        }, () => {
            this.setData({ buttonLoading: false })
        })
    },
    //选择地区
    cityChange({ detail }) {
        const [province, city, district] = detail.value
        const [province_code, city_code, county_code] = detail.code
        this.setData({
            province,
            province_code,
            city,
            city_code,
            district,
            county_code,
            address: province + city + district,
            region: detail.value,
            addressErr: false
        })
    },
    clearErr() {
        this.setData({
            addressErr: false,
            front_IDErr: false,
            reverse_IDErr: false,
            houseErr: false,
            logoErr: false,
            customErr: false
        })
    },
    //协议
    onAgreeChange({ detail }) {
        this.setData({ agreement: detail })
    }
})